// rongYun.js
var rongCloud;
var teacherGroupObjc = new Object();
var teacherGroupId = null;
var teacherGroupName = null;
//初始化融云
function initRongYun(callBack){
	rongCloud = api.require('rongCloud2');
	rongCloud.init(function(ret, err) {
	    if (ret.status == 'error'){
	        // api.toast({ msg: '融云初始化错误：'+err.code });
	    }
	});
}
//设置通知
function setupRongCloudMsgNotifi(){
	api.addEventListener({
	    name: 'RongCloudJoinGroupNotifi'
	}, function(ret, err){
	    var value = ret.value;
		joinChatGroup(value.groupId, value.groupName);
	});
}
//连接融云
function connectRongYun(){
	rongCloud.connect({
	    token: $api.getStorage('rongYunToken')
	},function(ret, err) {
	    if (ret.status == 'success'){

	    }else {
	    	// api.toast({ msg: '融云连接错误：'+err.code });
	    }
	});
}
//断开融云
function disConnectRongYun(){
	rongCloud.disconnect({
	    isReceivePush: true
	}, function(ret, err) {
	    if ('success' == ret.status) {
	        // api.toast({ msg: '断开连接成功!' });
	    }
	});
}
//接收消息
function receiveMsg(callBack){
	// alert('初始化');
	initRongYun();
	rongCloud.setOnReceiveMessageListener(function(ret, err) {
		if(callBack){
			callBack(ret, err);
		}
		api.sendEvent({
		    name: 'RongCloudNewMsgNotifi',
		    extra: {
		        msgRet: ret,
		        msgErr: err
		    }
		});
		if ($api.getStorage('cfnetSelectType') == '1') {
			var targetId = ret.result.message.targetId;
			if (targetId == teacherGroupId) {//接收到老师群组的消息
				api.sendEvent({
				    name: 'RongCloudTeacherGroupNewMessageNotifi',
				    extra: {
				        status: 1
				    }
				});
			}
		}
		// alert('接收到消息后发送通知');
	});
	connectRongYun();
	// setupConnectStatusLinster();
}
function setupConnectStatusLinster(){
	rongCloud.setConnectionStatusListener(function(ret, err) {
	    var connectionStatus = ret.result.connectionStatus;
	    if (connectionStatus == 'KICKED') {//用户账户在其他设备登录
	    	api.sendEvent({
	    	    name: 'CurrentUserChangeNotifi'
	    	});
	    }
	});
}
//加入群组
function joinChatGroup(groupId, groupName){
	rongCloud.joinGroup({
	    groupId: groupId,
	    groupName: groupName
	}, function(ret, err) {
	    if (ret.status == 'success'){
	        // api.toast({ msg: '加入群组成功：'+JSON.stringify(ret.status) });
	    }else{
	        // api.toast({ msg: '加入群组失败：'+err.code });
		}
	});
}
function getUserToken(){
	var params = {
		uid : $api.getStorage('cfnetppuid'),
		jid : $api.getStorage('cfnetppjxid')
	};
	// alert(JSON.stringify(params));
	cfnetppPOST(getRongCloudToken, params, true, function(data, status){
		// alert(JSON.stringify(data));
		if (status == 'success') {
			if (data.code == 200) {
				$api.setStorage('rongYunUid', data.userId);
				$api.setStorage('rongYunToken', data.token);
				receiveMsg();
				setupRongCloudMsgNotifi();
				if ($api.getStorage('cfnetSelectType') == '1') {//老师
					setupTeacherGroup(function(status){
						if (status == 1) {
							joinChatGroup(teacherGroupId, teacherGroupName);
						}
					});
				}
			}
		}
	});
}
//设置老师群组
function setupTeacherGroup(callBack){
	params = {
		jid : $api.getStorage('cfnetppjxid')
	};
	cfnetppPOST(getRongCloudTeacherGroup, params, true, function(data, status){
		if (status == 'success') {
			if (data.code == 200) {
				var groupData = data.data;
				// $api.setStorage('RongCloudTeacherGroupId', groupData.group);
				// $api.setStorage('RongCloudTeacherGroupName', groupData.groupname);
				teacherGroupId = groupData.group;
				teacherGroupName = groupData.groupname;
				var userList = groupData.uname;
				if (userList != null && userList != undefined && userList.constructor == Array && userList.length > 0) {
					for (var i = userList.length - 1; i >= 0; i--) {
						var userData = userList[i];
						teacherGroupObjc[userData.uid] = userData;
					}
					callBack(1);
				}else{
					callBack(0);
				}
			}else{
				callBack(0);
			}
		}else{
			callBack(0);
		}
	});
}